<template>
  <el-dialog title="发布产品"
             :visible.sync="visible"
             @close="handleCancelCreate">
    <el-form :model="createTemp" label-position="top">
      <!--<el-form-item label="发布时间" prop="">-->
        <!--&lt;!&ndash;<el-input v-model="updateTemp.publishedAt" type="text">&ndash;&gt;-->
        <!--&lt;!&ndash;</el-input>&ndash;&gt;-->
        <!--<el-date-picker-->
          <!--v-model="createTemp.publishedAt"-->
          <!--type="datetime"-->
          <!--format="yyyy-MM-dd HH:mm"-->
          <!--placeholder="选择日期时间">-->
        <!--</el-date-picker>-->
      <!--</el-form-item>-->
      <el-form-item label="产品名称" prop="">
        <el-input v-model="createTemp.name" type="text" placeholder="产品名称">
        </el-input>
      </el-form-item>
      <el-form-item >
        <p>产品图片</p>
        <el-upload
          class="img-uploader"
          name="url"
          ref="upload"
          action="string"
          :auto-upload=true
          :show-file-list="false"
          :on-change="handleImgChange"
          :before-upload="beforeImgUpload"
          :http-request="handleUpload"
        >
          <img v-if="imgUrl" :src="imgUrl" class="img" >
          <div v-else class="img-uploader-img">
            <i class="el-icon-plus "></i>
            <div class="upload-text">点击上传图片</div>
          </div>
        </el-upload>
        <!--<el-dialog :visible.sync="dialogVisible" :append-to-body="true">-->
          <!--<img width="100%" :src="dialogImageUrl" alt="">-->
        <!--</el-dialog>-->
      </el-form-item>
      <p class="tinyc-label">产品介绍</p>
      <div class="editor-container">
        <tinymce :height=400
                 ref="editor"
                 :init = "initContent"
                 v-model="createTemp.introduce"
        ></tinymce>
      </div>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCancelCreate">取消</el-button>
      <el-button type="primary" @click="handleCreateData">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import Tinymce from '@/components/Tinymce'

  export default {
    name: 'create-dialog',
    components: {
      Tinymce
    },
    data() {
      return {
        img: null,
        visible: false,
        imgUrl: '',
        dialogVisible: false
      }
    },
    props: {
      createTemp: Object,
      dialogCreateFormVisible: Boolean,
      initContent: Boolean
    },
    watch: {
      dialogCreateFormVisible(val) {
        this.visible = val
      },
      createTemp(val) {
        this.imgUrl = val.url
      }
    },
    methods: {
      handleCancelCreate() {
        this.visible = false
        this.$emit('cancel', this.visible)
      },
      handleCreateData() {
        if (this.img) {
          this.$emit('create', this.img)
          this.$refs.upload.clearFiles()
        } else {
          this.$message({
            message: '请上传图片',
            type: 'error'
          })
        }
      },
      handleUpload(data) {
        // console.log('111',data.file)
        this.img = data.file
      },
      handleImgChange(file) {
        this.imgUrl = file.url
        // console.log(file)
      },
      beforeImgUpload(file) {
        const isLt5M = file.size / 1024 / 1024 < 5

        if (!isLt5M) {
          this.$message({
            message: '上传图片大小不能超过 5MB!',
            type: 'error',
            duration: 3000
          })
        }
        return isLt5M
      }
    }
  }
</script>

<style scoped>

</style>
